package com.zjitc.controller.admincontroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.pojo.Admin;
import com.zjitc.pojo.Car;
import com.zjitc.pojo.User;
import com.zjitc.service.AdUserService;
import com.zjitc.service.impl.AdUserServiceImpl;
import com.zjitc.utils.PageBean;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/6/4 16:21
 * @description:
 */
@Controller
public class AdminController {

  @Autowired
  AdUserService adUserService=new AdUserServiceImpl();

  //登陆
  @RequestMapping(value = "/adminuserLogin")
  public String adminuserLogin(HttpServletRequest request, HttpServletResponse response){
    String adminuser=request.getParameter("adminuser");
    String adminpassword=request.getParameter("adminpassword");
    Admin user=adUserService.findByCondition(adminuser,adminpassword);
    HttpSession session=request.getSession();
    if (user!=null){
      session.setAttribute("admin",user);
      return "/admin/home.jsp";
    }else {
      return "/admin/login.jsp";
    }
  }
  //退出
  @RequestMapping("/loginOut")
  public String loginOut(HttpServletRequest request, HttpServletResponse response){
    HttpSession session=request.getSession();
    session.removeAttribute("admin");
    return "/admin/top.jsp";
  }
  //查询全部用户
  @RequestMapping("/adminUser")
  public String userList(HttpServletRequest request, HttpServletResponse response){
    List<User> list=adUserService.findUserList();
    request.setAttribute("list",list);
    return "/admin/user/list.jsp";
  }
  //商品管理
  @RequestMapping("/adminProduct")
  public ModelAndView adminProduct(@RequestParam(required = true, defaultValue = "1")Integer page){
    ModelAndView modelAndView=new ModelAndView();
    PageHelper.startPage(page,10);
    List<Car> productList=adUserService.findProductAll();
    PageInfo<Car> pageInfo=new PageInfo<Car>(productList);
    modelAndView.addObject("pg",pageInfo);
    modelAndView.setViewName("/admin/product/list.jsp");
    return modelAndView;
  }
  //商品添加跳转
  @RequestMapping("/addUIProduct")
  public ModelAndView addUIProduct(){
    ModelAndView modelAndView=new ModelAndView();
    modelAndView.setViewName("/admin/product/add.jsp");
    return modelAndView;
  }
  //商品添加
  @RequestMapping("/addProduct")
  public ModelAndView productAdd(HttpServletRequest request,Car product,
      @RequestParam("upload") MultipartFile upload) throws IOException {
    ModelAndView modelAndView=new ModelAndView();
    if(!upload.isEmpty()){
      String path=request.getServletContext().getRealPath("/products/5");
      String filename= UUID.randomUUID().toString().replace("-","")+upload.getOriginalFilename();
      File filePath=new File(path,filename);
      if(!filePath.getParentFile().exists()){
        filePath.getParentFile().mkdirs();
      }
      upload.transferTo(new File(path+File.separator+filename));
      product.setCarimages("products/5/"+filename);
      /*product.setPdate(new Date());*/
      adUserService.productAdd(product);
      /*modelAndView.setView(new RedirectView("/adminProduct.do"));*/
      modelAndView.setViewName("/adminProduct.do");
      return modelAndView;
    }else{
      modelAndView.addObject("error","未上传");
      modelAndView.setViewName("/addUIProduct.do");
      return modelAndView;
    }
  }
  @RequestMapping("/productEditUI")
  public ModelAndView productEditUI(Integer carid){
    ModelAndView modelAndView=new ModelAndView();
    Car product=adUserService.editProudctUI(carid);
    modelAndView.addObject("product",product);
    modelAndView.setViewName("/admin/product/edit.jsp");
    return modelAndView;
  }
  @RequestMapping("/productDelete")
  public ModelAndView productDelete(Integer carid){
    ModelAndView modelAndView=new ModelAndView();
    adUserService.productDeletePid(carid);
    modelAndView.setViewName("/adminProduct.do");
    return modelAndView;
  }
  @RequestMapping("/productEdit")
  public ModelAndView productEdit(HttpServletRequest request,Car product,@RequestParam("upload") MultipartFile upload)
      throws IOException {
    ModelAndView modelAndView=new ModelAndView();
    if(!upload.isEmpty()){
      String path=request.getServletContext().getRealPath("/products/5");
      String filename= UUID.randomUUID().toString().replace("-","")+upload.getOriginalFilename();
      File filePath=new File(path,filename);
      if(!filePath.getParentFile().exists()){
        filePath.getParentFile().mkdirs();
      }
      upload.transferTo(new File(path+File.separator+filename));
      product.setCarimages("products/5/"+filename);
      /*product.setCarintroduce(new Date());*/
      adUserService.productEdit(product);
      modelAndView.setViewName("/adminProduct.do");
      return modelAndView;
    }else{
      /*product.setCarintroduce(new Date());*/
      System.out.println(product);
      adUserService.productEdit(product);
      modelAndView.setViewName("/adminProduct.do");
      return modelAndView;
    }
  }
  @RequestMapping("/deleteUser")
  public ModelAndView deleteUser(Integer id){
    ModelAndView modelAndView=new ModelAndView();
    adUserService.deleteUser(id);
    modelAndView.setViewName("/admin/home.jsp");
    return modelAndView;
  }
}
